import { createRouter,createWebHashHistory } from "vue-router";
import Layout from "../views/layout/index.vue"
import store from "../stores/vuexStore"


export const routes=[
  {
    path:"/login",
    name:"login",
    lable:"登录",
    hidden:true,
    component:()=>import("@/views/login/index.vue")
  },
  {
    path:"/403",
    name:"no-permission",
    lable:"403",
    hidden:true,
    component:()=>import("@/views/noPermission/index.vue")
  },
  {
    path:"/",
    name:"alyout",
    redirect:"/alyout",
    lable:"首页",
    hidden:true,
    //避免../../，可以使用@符号，代表src目录
    component:Layout
  },
  {
    path:"/alyout",
    name:"alyout",
    lable:"首页",
    //避免../../，可以使用@符号，代表src目录
    component:Layout,
    children:[
      {
        path:"",
        name:"layout/default",
        lable:"默认",
        component:()=>import("../views/layout/components/default.vue")
      }
    ]
  },
  {
    path:"/banner",
    name:"banner",
    lable:"轮播图管理",
    component:Layout,
    children:[
      {
        path:"list",
        name:"banner/list",
        lable:"轮播图列表",
        component:()=>import("@/views/banner/List.vue")
      },
      {
        path:"add",
        name:"banner/add",
        lable:"添加轮播图",
        component:()=>import("@/views/banner/Add.vue")
      }
    ]
  },
  {
    path:"/maneger",
    name:"maneger",
    lable:"管理员管理",
    component:Layout,
    children:[
      {
        path:"list",
        name:"maneger/list",
        lable:"列表",
        component:()=>import("@/views/maneger/List.vue")
      },
      {
        path:"add",
        name:"maneger/add",
        lable:"添加",
        component:()=>import("@/views/maneger/Add.vue")
      },
      {
        path:"details",
        name:"maneger/details",
        lable:"详情",
        component:()=>import("@/views/maneger/Details.vue")
      },
    ]
  }
]

const router=createRouter({
  history:createWebHashHistory(),
  routes:routes
})

const whiteList=["login"]

router.beforeEach((to,from,next)=>{
  //console.log(to);
  const name=to.name
  if(whiteList.includes(name)){
    next()
  }else{
    //else需要登录验证，再判断是否有token，如果有，放行
    const token=localStorage.getItem("token")
    if(token){
      if(store.state.manegerData.role===2||store.state.manegerData.checkedkeys.includes(name)){
        next()
      }else{
        next("/403")
      }
      
    }
    else{
      next("/login")
    }
  }
})

export default router